<?php
/**
 * index.php
 * parse railadmin.tpl include all available modules.
 *
 * PHP versions 5
 *
 * Copyright (C)
 * This program is free software. You can redistribute it and/or modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation. Either version 2 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY. Without even the implied warranty
 * of ERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with this program. Ff not, write to the Free Software
 * Fundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110, USA
 *
 * Copyright (C) 2006-2007
 *
 * @author Bastian Schröder <bs@chemanet.de>
 * @version 2008-04-10
 * @package railadmin
 */

session_start();

// set default exception handler
function exception_handler( $exception ) {
	print_r( '<!--' . $exception . '-->' );
	die( 'Uncaught exception: ' . $exception->getMessage() );
}
set_exception_handler('exception_handler');

// check dependency
if(
	!is_file( 'configs/railadmin.conf.php' ) ||
	!is_writable( 'cache/' )
){
	echo nl2br( file_get_contents( 'configs/README' ) );
	echo '<br /><button onclick="location.href=\'index.php\';">Reload</button>';
	exit;
}

// includes
require_once( 'configs/railadmin.conf.php' );
require_once( "$cfg_pahToPHPrail/rail_connector.class.php" );
require_once( 'resources/smarty-2.6.18/Smarty.class.php' );

// listen: logout
if( isset($_GET['logout']) ){
	session_destroy();
	header( 'Location: index.php' );
}

// include Smarty libary
$smarty = new Smarty();
$smarty->caching = false;
$smarty->template_dir = './';
$smarty->compile_dir = 'cache/';
$smarty->config_dir = 'cache/';
$smarty->cache_dir = 'cache/';
$smarty->left_delimiter = '{{';
$smarty->right_delimiter = '}}';

// define version and get svn revision
define('RAILADMIN_VERSION', '2.1');
define('RAILADMIN_REVISION', 'beta');

// connect
include('configs/connect.php');


/* ------------------------------------- modules -------------------------------------- */
$modules = array();
$dirhandle = opendir( 'modules/' );
while( $file = readdir($dirhandle) ) {
	if( substr( $file, 0, 1 ) == '.' || $file == 'folder.ini' || $file == 'Thumbs.db' || $file == 'README' ) continue;
	// register module
	$mod_conf = array();
	include_once( "modules/$file/configs/module.conf.php" );
    $modules[] = array(
    	'name' => isset($mod_conf['name']) ? $mod_conf['name'] : $file,
    	'summary' => $mod_conf['summary'],
    	'icon' => "modules/$file/icon.png"
    );
}
usort($modules, 'cmp_modules');
closedir( $dirhandle );
$smarty->assign( "modules", $modules );


/* -------------------------------------- patche -------------------------------------- */
foreach( array_reverse(path2array('../patch/')) as $file ){
	$version = explode( 'to', substr($file, 5, -3) );
	$patches[] = array(
		'name' => substr($file, 0, -3),
		'file' => $file,
		'size' => filesize( '../patch/' . $file ),
		'from' => $version[0],
		'to' => $version[1],
	);
}
$smarty->assign( "patches", $patches );


/* ------------------------------------- display -------------------------------------- */
// define status
if( isset( $_SESSION['railadmin_MYSQL_host'] ) ){
	$status = $_SESSION['railadmin_MYSQL_user'] . '@' . $_SESSION['railadmin_MYSQL_host'];
	if( isset( $_SESSION['railadmin_MYSQL_db'] ) )
		$status .= ':' . $_SESSION['railadmin_MYSQL_db'];
}
else $status = 'nicht verbunden.';

// pasrse out
$smarty->assign( array(
	'railversion' => PHPrail_VERSION . '-' . PHPrail_REVISION,
	'railadminversion' => RAILADMIN_VERSION . '-' . RAILADMIN_REVISION,
	'status' => $status,
	'max_upload_size' => ini_get( 'upload_max_filesize' ) <  ini_get( 'post_max_size' ) ? ini_get( 'upload_max_filesize' ) :  ini_get( 'post_max_size' ),

	'mysql' => array(
		'host' => isset($_SESSION['railadmin_MYSQL_host']) ? $_SESSION['railadmin_MYSQL_host'] : 'localhost',
		'user' => isset($_SESSION['railadmin_MYSQL_user']) ? $_SESSION['railadmin_MYSQL_user'] : '',
		'passwd' => isset($_SESSION['railadmin_MYSQL_passwd']) ? $_SESSION['railadmin_MYSQL_passwd'] : '',
		'db' => isset($_SESSION['railadmin_MYSQL_db']) ? $_SESSION['railadmin_MYSQL_db'] : ''
	)

));

// display
$smarty->display( 'railadmin.tpl' );


/* ------------------------------------ functions ------------------------------------- */

function path2array( $pPath ){
	$files = array();
	$fp = opendir($pPath);
	while( $file = readdir($fp) ){
		if( substr( $file, 0, 1 ) != "." &&
			$file != "folder.ini" &&
			$file != "Thumbs.db" &&
			$file != "README"
		) $files[] = $file;
	}
	return $files;
}


function cmp_modules($a, $b){
	if( $a['name'] == 'railbrowser' )
		return -1;
	if( $a['name'] == 'users' && $b['name'] != 'railbrowser' )
		return -1;

	return 1;
}

?>